package com.gw.business.statistics.attribution.controller;

import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gw.business.analysis.model.UserTrackDataDetail;
import com.gw.business.analysis.service.UserTrackDataDetailService;
import com.gw.business.common.util.DateUtil;
import com.gw.business.common.util.excel.ConditionCollection;
import com.gw.business.common.util.excel.ExcelOperate;
import com.gw.business.statistics.detailed.model.DetailForm;
import com.gw.platform.controller.DefaultPageController;
import com.gw.platform.page.impl.Page;
import com.gw.platform.util.DateUtils;
import com.gw.platform.util.StringUtils;


/**
 * 统计报表
 * @author  kidy
 * @since   1.0
 * @version 2015年2月4日 kidy
 */
@Controller
@RequestMapping("attribution")
public class UserTrackAttributionController  extends DefaultPageController{
    final static Logger logger  = Logger.getLogger(UserTrackAttributionController.class);
    @Resource
    private UserTrackDataDetailService userTrackDataDetailService;
    
    private ExcelOperate excelOperate;
    /**
     * 详情列表
     * 
     * @return
     */
    @RequestMapping("/page")
    public String queryAttribution() {
        try {
            String pageNum = request.getParameter("pageNum");
            String pageSize = request.getParameter("numPerPage");
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            String utmcsr = request.getParameter("utmcsr");
            String utmcmd = request.getParameter("utmcmd");
            String tempAttr=request.getParameter("tempAttr");
            String defAttrStartTime=request.getParameter("defAttrStartTime");
            String defAttrEndTime=request.getParameter("defAttrEndTime");
            if (StringUtils.isEmpty(pageNum)) {
                pageNum = "1";
                pageSize = Page.DEFAULT_PAGE_SIZE + "";
            }
         // 设置当前页
            setCurrent(Long.parseLong(pageNum));
            Map<String, Object> map = new HashMap<String, Object>();
            
            if (tempAttr == null) {
                
                startTime = DateUtil.getDateFormat(DateUtil.getBeforeDay(new Date(), 1), "yyyy-MM-dd");
                endTime = DateUtil.getDateFormat(DateUtil.getBeforeDay(new Date(), 1), "yyyy-MM-dd");
                defAttrStartTime=startTime;
                defAttrEndTime=endTime;
            }
            if (startTime != null && startTime != "") {
                map.put("startTime", DateUtils.stringToDate(startTime,"yyyy-MM-dd"));
                
            }
            if (endTime != null && endTime != "") {
                map.put("endTime", DateUtils.stringToDate(endTime,"yyyy-MM-dd"));
                
            }
            if (utmcsr != null && utmcsr != "") {
                map.put("utmcsr", utmcsr);
                
            }
            if (utmcmd != null && utmcmd != "") {
                map.put("utmcmd", utmcmd);
                
            }
            // 查询用户列表
            page = defaultService.page("queryAttributionData", "queryAttributionDataCount", getCurrent(), map, Integer.parseInt(pageSize));
            
            request.setAttribute("page", page);
            request.setAttribute("startTime", startTime);
            request.setAttribute("endTime", endTime);
            request.setAttribute("defAttrStartTime", defAttrStartTime);
            request.setAttribute("defAttrEndTime", defAttrEndTime);
            request.setAttribute("utmcsr", utmcsr);
            request.setAttribute("utmcmd", utmcmd);
        } catch (Exception e) {
          logger.error("queryAttribution exception",e);
        }
        return "statisc/attribution/queryAttributionPage";
    }
    
    /**
     * 导出
     * 
     * @param req
     * @param response
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping("/export")
    public void export(HttpServletRequest req, HttpServletResponse response, DetailForm detailForm) {
        OutputStream out = null;
        try {
            String fileName = "来源媒介列表";
            String fileName2 = new String(fileName.getBytes("gb2312"), "iso-8859-1");
            // 文件名
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            response.setHeader("content-disposition", "attachment;filename=" + fileName2 + ".xls");
            
            String pageNum = request.getParameter("pageNum");
            String pageSize = request.getParameter("numPerPage");
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            String utmcsr = request.getParameter("utmcsr");
            String utmcmd = request.getParameter("utmcmd");
            if(StringUtils.isEmpty(pageNum)){
                pageNum = "1";
                pageSize = Page.DEFAULT_PAGE_SIZE + "";
            }
         // 设置当前页
            setCurrent(Long.parseLong(pageNum));
            Map<String, Object> map = new HashMap<String, Object>();
            if (startTime != null && startTime != "") {
                map.put("startTime", DateUtils.stringToDate(startTime,"yyyy-MM-dd"));
                
            }
            if (endTime != null && endTime != "") {
                map.put("endTime", DateUtils.stringToDate(endTime,"yyyy-MM-dd"));
                
            }
            if (utmcsr!=null&&utmcsr!="") {
                map.put("utmcsr", utmcsr);
              
            } 
            if (utmcmd!=null&&utmcmd!="") {
                map.put("utmcmd", utmcmd);
              
            } 
            page = defaultService.page("queryAttributionData", "queryAttributionDataCount", getCurrent(), map, Integer.parseInt(pageSize));
            excelOperate = new ExcelOperate();
            excelOperate.setDataList((List<UserTrackDataDetail>) page.getData());
            excelOperate.setThAndFieldMap(ConditionCollection.getAttributionTHFieldMap());
            excelOperate.excelExportTemp(out, response);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.flush();
                out.close();
            } catch (Exception e2) {
                
            }
        }
        
    }
   
}
